package me.work.pay.jsyl.mvp.presenter;

import android.app.Application;
import android.content.Intent;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.work.pay.jsyl.app.Constants;
import me.work.pay.jsyl.app.utils.RxUtil;
import me.work.pay.jsyl.mvp.contract.SplashContract;
import me.work.pay.jsyl.mvp.model.AppModel;
import me.work.pay.jsyl.mvp.model.entity.LauncherData;
import me.work.pay.jsyl.mvp.ui.activity.GroupListActivity;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 11/04/2020 21:59
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class SplashPresenter extends BasePresenter<SplashContract.Model, SplashContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public SplashPresenter(SplashContract.Model model, SplashContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void init(String sn) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("sn", Constants.sn);
        Observable.just(1).subscribeOn(Schedulers.io())
                .timeout(10, TimeUnit.SECONDS)
                .flatMap(new Function<Integer, ObservableSource<LauncherData>>() {
                    @Override
                    public ObservableSource<LauncherData> apply(Integer integer) throws Exception {
                        return mModel.init(map, NetworkUtils.isConnected());
                    }
                })
                .compose(RxUtil.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<LauncherData>(mErrorHandler) {
                    @Override
                    public void onNext(LauncherData result) {
                        if (result.isSuccess()) {
                            AppModel.getInstance().saveLauncherData(result);
                        } else {
                            if (ObjectUtils.isNotEmpty(result.error_msg))
                                mRootView.showMessage(result.error_msg);
                            AppModel.getInstance().saveLauncherData(new LauncherData());
                        }
                        mRootView.launchActivity(new Intent(mRootView.getActivity(), GroupListActivity.class));

                    }

                    @Override
                    public void onError(Throwable t) {
//                        super.onError(t);
                        mRootView.launchActivity(new Intent(mRootView.getActivity(), GroupListActivity.class));
                        AppModel.getInstance().saveLauncherData(new LauncherData());
                    }
                });


    }
}
